/**
* Created by ren on 2017/1/6.
*/
<template>
  <div class='addressList' v-if="addressData!==''">
    <div class="item" v-for="(item,index) in addressData">
      <div>
        <span>{{item.consigneeName}}：{{item.mobile}}</span> <span v-if="item.isDefault===1">默认地址</span>
      </div>
      <div>{{item.province}} {{item.city}} {{item.area}} {{item.detailAddress.substring(0,30)}}</div>
      <div>
        <img :src="item.isDefault===0?require('../../assets/hollow.png'):require('../../assets/true3.png')"
             style="margin-right: 10px;width: 20px;cursor: pointer"
             @click="updateDefault(item.id,item.isDefault,index)">
        <span>设置为默认地址</span>
        <span style="margin-left: 339px">
        <button class="btn" style="margin-right: 30px" @click="update(index)">编辑</button>
        <button class="btn" style="background: #ff0000" @click="del(item.id)">删除</button>
      </span>
      </div>
    </div>
    <div style="margin-top: 10px;position: absolute;top: 36px;right: 15px;font-size: 18px">
      <span style="float:right;color:#e6760a;cursor: pointer" @click="create()">新建收货地址</span>
    </div>
    <Notify :parentThis='this'></Notify>
  </div>
</template>

<script>
  import API from '../../api'
  import Notify from '../Notify'
  export default {
    name: 'addressList',
    components: {
      Notify
    },
    data: function () {
      return {
        notifyStatus: 2,
        addressData: ''
      }
    },
    props: ['_parentThis'],
    methods: {
      findAddressList: function () {
        this.$http.post(API.findAddressList, {
          userId: this.$cookie.get('id'),
          nextPage: 0,
          pageSize: 10000,
          type: 2,
          qishu: 1
        }).then((ret) => {
          const result = ret.body
          if (result.code === 1) {
            this.addressData = result.object
          } else {
            this.notifyStatus = '0|' + result.message
          }
        }, (err) => {
          console.log(err)
        })
      },
      updateDefault: function (id, isDefault) {
        if (isDefault === 1) {
          return
        }
        this.$http.post(API.updateAddress, {
          id: id,
          isDefault: 1
        }).then((ret) => {
          const result = ret.body
          if (result.code === 1) {
            this.findAddressList()
          } else {
            this.notifyStatus = '0|' + result.message
          }
        }, (err) => {
          console.log(err)
        })
      },
      update: function (index) {
        this.$cookie.set('addressData', JSON.stringify(this.addressData[index]))
        this._parentThis.safeChild = 'updateAddress'
        this._parentThis.titHtml = '<span>信息安全 > <span>地址管理</span> > <span style="color: #f48315;">修改地址</span></span>'
      },
      create: function () {
        this._parentThis.safeChild = 'createAddress'
        this._parentThis.titHtml = '<span>信息安全 > <span>地址管理</span> > <span style="color: #f48315;">添加地址</span></span>'
      },
      del: function (id) {
        this.$http.post(API.operateAddressById, {
          ids: id
        }).then((ret) => {
          const result = ret.body
          if (result.code === 1) {
            this.findAddressList()
          } else {
            this.notifyStatus = '0|' + result.message
          }
        }, (err) => {
          console.log(err)
        })
      }
    },
    mounted: function () {
      this.findAddressList()
    }
  }
</script>
